<template>
<div>
  <el-form :model="formData"  inline
      class="myForm">
    <el-form-item prop="subjectsName">
      <el-input v-model="formData.subjectsName" placeholder="请输入检查项目名称"></el-input>
    </el-form-item>
     <el-form-item>
       <el-button @click="search" type="primary" icon="el-icon-search">查找</el-button>
        <el-button @click="openDialog('创建', 'add', null)" type="success"
         icon="el-icon-plus">添加</el-button>
      </el-form-item>
  </el-form>



    <el-table :data="tableData" border class="myTab">
      <el-table-column label="项目id" prop="subjectsId" v-if="false"></el-table-column>
      <el-table-column label="项目名称" prop="subjectsName" align="center"></el-table-column>
      <el-table-column label="项目检查地址" prop="subjectAddress" align="center"></el-table-column>
      <el-table-column label="项目价格" prop="subjectsPrice" align="center"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        >
        <template slot-scope="scope">
 <el-button type="primary" icon="el-icon-edit" circle @click="openDialog('编辑', 'edit', scope.row)"></el-button>
            <el-button type="danger" 
            icon="el-icon-delete" circle @click="deleteData(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

       <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 50, 100]"
      :page-size="pageInfo.limit"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      class="myStyle"
      >
      </el-pagination>

  <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      width="500"
    >
      <el-form ref="dialogForm" :model="dialogFormData" label-width="100px"  :rules="formRules">
        <el-form-item label="科目名称" prop="subjectsName">
          <el-input v-model="dialogFormData.subjectsName"></el-input>
        </el-form-item>
        <el-form-item label="科目地址" prop="subjectAddress">
          <el-input v-model="dialogFormData.subjectAddress"></el-input>
        </el-form-item>  
        <el-form-item label="价格" prop="subjectsPrice">
          <el-input v-model="dialogFormData.subjectsPrice"></el-input>
        </el-form-item>   
      </el-form>

      <span slot="footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </span>
    </el-dialog>
    </div>
</template>


<script>
import axios from "axios";

export default {
  name: "subject",
  comName: "科目管理",
  data() {
    return {
      tableData: [],
      tableLoading: false,
      dialogVisible: false,
      dialogTitle: '创建',
      dialogType: 'add',
      formData: {
        subjectsName: "",
      },
      dialogFormData: {
        subjectsName:'',
        subjectAddress:'',
        subjectsPrice:'',
        subjectsId:''
      },
      pageInfo: {
        page: 1,
        limit: 10,
        total: 0,
      },
      formRules: {
        subjectsName: [{ required: true, trigger: "change",message: "请确认正确的信息"  }],
        subjectAddress: [{ required: true, trigger: "change", message: "请确认正确的信息"  }],
        subjectsPrice: [{ required: true, trigger: "change", message: "请确认正确的信息"  }],
      },
    }
  },
  mounted() {
    this.search();
  },  
  filters: {
    filterTableItem (value, options) {
      let index = options.findIndex(item => {
        return item.value === value
      })
      
      if (index < 0) {
        return '---'
      } else {
        return options[index].label
      }
    }
  },
  methods: {
    openDialog (title, type, row) {
      this.dialogVisible = true
      this.dialogType = type
      this.dialogTitle = title

      if (type === 'edit') {
        this.$nextTick(() => {
          this.dialogFormData = Object.assign({}, row)
        })
      }
    },
    confirm () {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          if (this.dialogType === 'add') {
            this.addData()
          } else if (this.dialogType === 'edit') {
            this.editData()
          }
        } else {
          return false
        }
      })
    },
    cancel () {
      this.$refs.dialogForm.resetFields()
      this.dialogVisible = false
    },

    addData(){
      axios({
        url:'/api/admin/addSubject',
        method:'post',
        data:{
          ...this.dialogFormData
        },
         headers: {
             token: sessionStorage.getItem("Token"),
          }
      }).then(res=>{
         if (res.data.error === 0) {
          this.cancel()
          this.$notify.success({title: '添加成功'})

          this.search()
        } else {
          this.$notify.error({title: '添加失败'})
        }
      })
    },
     editData () {
      axios({
        url: '/api/admin/updateSubject',
        method: 'post',
        data: {
          ...this.dialogFormData
        },
        headers: {
          token: sessionStorage.getItem("Token"),
        }
      }).then(res => {
        if (res.data.error === 0) {
          this.cancel()
          this.$notify.success({title: '编辑成功'})

          this.search()
        } else {
          this.$notify.error({title: '编辑失败'})
        }
      })
    },
    deleteData (row) {
      this.$confirm('此操作将删除这条信息, 是否继续?').then(() => {
        axios({
          url: "/api/admin/deleteSubject",
          method: 'post',
          data: {
            subjectsId:row.subjectsId
          },
          headers: {
             token: sessionStorage.getItem("Token"),
          }
        }).then(res => {
          this.$notify.success({title: '删除成功'})
          this.search()
        })
      })
    },
    search () {
      this.tableLoading = true

      axios({
        url: "/api/admin/searchSubject",
        method: "get",
        params: {
          ...this.formData,
          limit:this.pageInfo.limit,
          page:this.pageInfo.page,
          
        },
        headers: {
          token: sessionStorage.getItem("Token"),
        },
      }).then(res => {
        console.log(res);
        this.tableData = res.data.list;
        this.tableLoading = false;

        this.pageInfo.total=res.data.total
          
      });
    },
    // 每页多少条切换
    handleSizeChange (value) {
      this.pageInfo.limit = value

      this.search()
    },
    // 当前页码的切换
    handleCurrentChange (value) {
      this.pageInfo.page = value

      this.search()
    }
  }
}
</script>

<style lang="scss" scoped>
.myTab {
  width: 95%;
  text-align: center;
  margin: 0 auto;
}

.myForm{

margin-top: 30px;
margin-right: 29px;
text-align: right;
}
.myStyle {
  margin-top: 40px;
  text-align: center;
}
</style>
